import PageBase from "../../../Game/Base/PageBase";
import { IPagePramsBase } from "../../../Game/Interface";
import O1HD from "../../../engines/gameEngine";
import { CsvFileKind } from "../../../engines/libs/CsvFileKind";
import data_control from "../../../engines/libs/data_control";
import level_goldMiner from "../level_goldMiner";
import level_goldMiner_shopItem from "../other/Level_goldMiner_shopItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class level_goldMinerShopPage extends PageBase {

    @property({type:cc.Node,tooltip:"道具节点"})
    propItem:cc.Node = null;

    @property({type:cc.Node,tooltip:"道具content"})
    propContent:cc.Node = null;

    @property({type:cc.Node,tooltip:"大节点"})
    shopNode:cc.Node = null;

    @property({type:cc.Node,tooltip:"按钮节点"})
    btnNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"当前金额文本"})
    lb_curMoney:cc.Label = null;

    level:level_goldMiner = null;

    completeFun:()=>void = null;

    start(){
        this.shopAni();
    }

    bindLevel(level:level_goldMiner,completeFun:()=>void){
        this.level = level;
        this.completeFun = completeFun;
        this.loadShop();
        this.refrenshCurMoney();
    }

    shopAni(){
        cc.tween(this.shopNode).to(1,{x:0},{easing:cc.easing.backOut}).call(()=>{
            cc.tween(this.btnNode).to(0.5,{opacity:255}).start();
        }).start();
    }

    loadShop(){
        let allPropIds:string[] = O1HD.getManagers().csvDataManager.getCsvIds(CsvFileKind.CsvFile.goldMinerProp);
        let moneyPropAry:string[] = [];
        let adProp:string[] = [];
        for(let id of allPropIds){
            let prop = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.goldMinerProp,id);
            if(prop["ad_flag"] == "1"){
                adProp.push(id);
            }else{
                moneyPropAry.push(id);
            }
        }
        adProp = data_control.randomSortArray(adProp);
        moneyPropAry = data_control.randomSortArray(moneyPropAry);
        let propIds:string[] = [];
        let randCount:number = Math.floor(Math.random() * 2 ) + 2;
        let randIndex:number = Math.floor(Math.random() * randCount);
        for(let i = 0;i < randCount;i++){
            let propId = null;
            if(randIndex == i){
                propId = adProp.pop();
            }else{
                propId = moneyPropAry.pop();
            }
            propIds.push(propId);
        }

        for( let i =0; i < propIds.length; i++){
            let id = propIds[i];
            let item = O1HD.getManagers().csvDataManager.getCsvDataItem(CsvFileKind.CsvFile.goldMinerProp,id);
            let propItem = cc.instantiate(this.propItem);
            let ps = propItem.getComponent(level_goldMiner_shopItem);
            ps.bindData(item,this.level);
            propItem.parent = this.propContent;
            propItem.on("touchend",this.onSelectOne,this);
        }

    }

    onSelectOne(e:cc.Event.EventTouch){
        let node:cc.Node = e.target;
        let ns = node.getComponent(level_goldMiner_shopItem);
        let nData = ns.data;
        if(ns.buyFlag){
            O1HD.getModules().uiControlModule.showMessageBox("已售罄");
            return;
        }
        if(nData["ad_flag"] == "1"){
            // 需要看广告
            if(O1HD.getServices().userSrv.getCoins() > 0){
                O1HD.getServices().userSrv.setCoins(-1,true);
                ns.onBuy();
                this.buyOneItem(ns,nData);
            }else{
                O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                    ns.onBuy();
                    this.buyOneItem(ns,nData);
                },()=>{

                },null,`看广告获得道具${nData["prop_name"]}`);
            }
        }else{
            if(this.level.curMoney >= ns.moneyNum){
                ns.onBuy();
                this.level.curMoney -= ns.moneyNum;
                this.refrenshCurMoney();
                this.buyOneItem(ns,nData);
            }else{
                O1HD.getModules().uiControlModule.showMessageBox("钱不够");
                return;
            }
        }
    }

    refrenshCurMoney(){
        this.lb_curMoney.string = this.level.curMoney.toString();
    }

    buyOneItem(item:level_goldMiner_shopItem,data:any){
        let icon = item.icon;
        let pos = this.node.convertToNodeSpaceAR(icon.node.convertToWorldSpaceAR(cc.v2()));
        icon.node.setParent(this.node);
        icon.node.setPosition(pos);
        //
        let targetPos:cc.Vec2 = cc.v2(200,159);
        //@ts-ignore
        cc.tween(icon.node).to(0.2,{position:targetPos}).to(0.2,{opacity:0}).call(()=>{
            O1HD.getManagers().goldMinerManager.addOneProp(Number(data["prop_type"]));
            if(data["prop_type"] == "4"){
                this.level.showSpeedUpgrade();
            }
            if(item && item.node && item.node.isValid){
                icon.node.setParent(item.node);
                icon.node.setPosition(0,55);
            }else{
                icon.node.destroy();
                icon.node.removeFromParent();
            }
        }).start();
    }


    async OnCloseBtn(){
        this.completeFun && this.completeFun();
        super.OnCloseBtn();
    }
    
}
